package com.cy.aircompressor.main.view.chart;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;

import com.cy.aircompressor.entity.linechart.LineChartItemEntry;
import com.cy.aircompressor.entity.sensor.Sensor;
import com.cy.aircompressor.entity.sensor.SensorAgg;
import com.cy.aircompressor.entity.sensor.SensorLatestData;
import com.cy.aircompressor.entity.sensor.SensorStatisticsData;
import com.cy.aircompressor.main.home.fragment.v2.LineChartHelper;
import com.drug.lib.util.DensityUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class LineChartScrollView extends NestedScrollView {
    private Context mContext;
    private LinearLayout contentView;
    private LinearLayout.LayoutParams mTabParams;
    private List<LineChartView> mItems = new ArrayList<>();
    private List<LineChartItemEntry> itemEntries;

    public LineChartScrollView(@NonNull Context context) {
        this(context, null);
    }

    public LineChartScrollView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LineChartScrollView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        contentView = new LinearLayout(context);
        contentView.setBackgroundColor(Color.TRANSPARENT);
        contentView.setOrientation(LinearLayout.VERTICAL);
        contentView.setGravity(Gravity.CENTER);
        addView(contentView, new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        mTabParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mTabParams.topMargin = DensityUtil.dip2px(10);
    }

    public void initView(List<LineChartItemEntry> itemEntries) {
        if (itemEntries == null) return;
        if (itemEntries.size() == 0) return;
        this.itemEntries = itemEntries;
        itemEntries.forEach(new Consumer<LineChartItemEntry>() {
            @Override
            public void accept(LineChartItemEntry lineChartItemEntry) {
                LineChartView lineChartView = new LineChartView(mContext);
                lineChartView.init(lineChartItemEntry);
                lineChartView.setLayoutParams(mTabParams);
                contentView.addView(lineChartView);
                mItems.add(lineChartView);
                requestLayout();
            }
        });
    }


    public void updateCompressor(String compressorId) {
        itemEntries.forEach(new Consumer<LineChartItemEntry>() {
            @Override
            public void accept(LineChartItemEntry lineChartItemEntry) {
                lineChartItemEntry.setCompressorId(compressorId);
            }
        });
    }

    public List<LineChartItemEntry> getItemEntries() {
        return itemEntries;
    }


    public void setSensors(List<Sensor> sensors) {
        LineChartHelper.get().setSensorToList(this.itemEntries, sensors);
    }

    public void setSensorLatestData(SensorLatestData latestData) {
        LineChartHelper.get().sensorLatestDataToList(this.itemEntries, latestData);
    }

    public void setSensorStatisticsData(List<SensorStatisticsData> sensorStatisticsData) {
        LineChartHelper.get().sensorStatisticsDataToList(this.itemEntries, sensorStatisticsData);
    }

    public void setStartEndTime(String startTime, String endTime) {
        itemEntries.forEach(new Consumer<LineChartItemEntry>() {
            @Override
            public void accept(LineChartItemEntry lineChartItemEntry) {
                lineChartItemEntry.setStartTime(startTime);
                lineChartItemEntry.setEndTime(endTime);
            }
        });
    }

    public void resetAllDatas(boolean isResetSensor) {
        itemEntries.forEach(new Consumer<LineChartItemEntry>() {
            @Override
            public void accept(LineChartItemEntry lineChartItemEntry) {
                lineChartItemEntry.setSensorLatestData(null);
                resetSensor(lineChartItemEntry, isResetSensor);
            }
        });
        notifyAllDataSetChanged();
    }

    private void resetSensor(LineChartItemEntry lineChartItemEntry, boolean isResetSensor) {
        if (isResetSensor) {
            lineChartItemEntry.setSensorAggs(null);
            return;
        }
        List<SensorAgg> sensorAggs = lineChartItemEntry.getSensorAggs();
        if (sensorAggs == null) return;
        sensorAggs.forEach(new Consumer<SensorAgg>() {
            @Override
            public void accept(SensorAgg sensorAgg) {
                sensorAgg.setSensorStatisticsData(null);
                sensorAgg.setLineChartEntry(null);
            }
        });
    }

    public void notifyAllDataSetChanged() {
        mItems.forEach(new Consumer<LineChartView>() {
            @Override
            public void accept(LineChartView chartItem) {
                chartItem.notifyDataSetChanged();
            }
        });
    }


}
